//
//  WKJSHelper.m
//  BaseProject
//
//  Created by 王洋 on 2021/5/25.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "TTUDIDManager.h"
#import "HomeADItemEventModel.h"
#import "WKJSRequestModel.h"

#import "WeddingCarCartModel.h"

#import "SubmitCarOrderViewController.h"

#import "TT_WebviewController.h"

#import "WKJSHelper.h"

@implementation WKJSHelper

+ (void)handle_H5ToNativeWithData:(id)data
                               vc:(TT_WebviewController *)vc
                       completion:(WKJSHelperCallBack)completion {
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dic = data;

        WKJSRequestModel *model = [WKJSRequestModel modelWithJSON:dic];
        switch (model.request) {
            case TT_H5RequestType_NONE:{} break;
            case TT_H5RequestType_INFO:{
                model.ext = @{}.mutableCopy;
                
                NSString *scaId = [TTUserModel sharedObject].installModel.scaId;
                [model.ext setValue:scaId.length>0?scaId:@"" forKey:@"channel"];//channel
                
                [model.ext setValue:[Utilities getDeviceIdentifier] forKey:@"deviceModel"];//device
                NSString *systemVersion = [NSString stringWithFormat:@"IOS%.2f",[[[UIDevice currentDevice] systemVersion] floatValue]];
                [model.ext setValue:systemVersion forKey:@"systemVersion"];//sys version
                [model.ext setValue:[TTUDIDManager getUniqueDeviceID] forKey:@"deviceUniqueNo"];//idfa
                [model.ext setValue:APP_VERSION forKey:@"appVersionCode"];//app version
                [model.ext setValue:@"2" forKey:@"platformId"];//1-安卓，2-ios
                if ([TTUserModel sharedObject].installModel.scaId.length == 0) {
                    [model.ext setValue:@"apple" forKey:@"appStore"];//1-安卓，2-ios
                }
                
                [model.ext setValue:[TTUserModel sharedObject].userId forKey:@"userId"];//用户id
                [model.ext setValue:[TTUserModel sharedObject].token forKey:@"token"];//用户id

//                [model.ext setValue:@([TTUserModel sharedObject].longitude) forKey:@"longitude"];//用户id
//                [model.ext setValue:@([TTUserModel sharedObject].latitude) forKey:@"latitude"];//用户id

                
                model.success = 1;
                NSDictionary *callBackDic = [model modelToJSONObject];
                completion(callBackDic);
                
//                [vc.bridge callHandler:@"callBack_NativeToH5" data:callBackDic];
                
            } break;
            case TT_H5RequestType_JUMP:{
                NSDictionary *jumpDic = model.ext;
                if ([jumpDic isKindOfClass:[NSDictionary class]]) {
                    HomeADItemEventModel *actionModel = [HomeADItemEventModel modelWithJSON:jumpDic];
                    [[TT_JumpManager sharedManager] jumpWithModel:actionModel completion:^(BOOL success, NSString * _Nonnull message) {
                        completion(@{@"success":@(1),@"message":message});
                    }];
                }
            } break;
            case TT_H5RequestType_CarBUY:{
                NSDictionary *businessDic = model.ext;
                if ([businessDic isKindOfClass:[NSDictionary class]]) {
                    //一个购物车的模型
                    WeddingCarCartModel *model = [WeddingCarCartModel modelWithJSON:businessDic];
                    double platformPrice = model.platformPrice;
                    if (model.goodsNum > model.defaultNum) {
                        platformPrice = model.platformPrice + (model.goodsNum - model.defaultNum)*model.followPrice;
                    } else {
                        platformPrice = model.platformPrice;
                    }
                    model.totalPrice = platformPrice;
                    NSMutableArray <WeddingCarCartModel *>*array = @[].mutableCopy;
                    [array addObject:model];
                    SubmitCarOrderViewController *submitVc = [[SubmitCarOrderViewController alloc]init];
                    submitVc.dataArray = array.mutableCopy;
                    [vc.navigationController pushViewController:submitVc animated:YES];
                    completion(@{@"success":@(1),@"message":@"已成功接收到请求"});
                }
            } break;
            default:{
                completion(@{@"success":@(0),@"message":@"未定义的类型"});
            } break;
        }
        
    } else {
        completion(@{@"success":@(0),@"message":@"data无法识别"});
    }
}

@end
